package dao_sys

import (
	"wrblog-api-go/app/model"
	"wrblog-api-go/app/model/model_sys"
	"wrblog-api-go/pkg/errs"
	"wrblog-api-go/pkg/mysql"
	"wrblog-api-go/pkg/utils"
)

func GetMenuList(selectSysMenu model_sys.SelectSysMenu) (sysMenuList []model_sys.SysMenuVo, err error) {
	db := mysql.Db().Model(&model_sys.SysMenuPo{}).Table("sys_menu sm").Select("DISTINCT sm.*").
		Joins("left join sys_role_menu srm on srm.menu_id = sm.menu_id").
		Joins("left join sys_user_role sur on sur.role_id = srm.role_id")
	if selectSysMenu.MenuName != "" {
		db = db.Where("sm.menu_name like ?", "%"+selectSysMenu.MenuName+"%")
	}
	if selectSysMenu.Status != "" {
		db = db.Where("sm.status = ?", selectSysMenu.Status)
	}
	if selectSysMenu.UserId != nil {
		db = db.Where("sur.user_id = ?", selectSysMenu.UserId)
	}
	if selectSysMenu.RoleId != nil {
		db = db.Where("srm.role_id = ?", selectSysMenu.RoleId)
	}
	if !selectSysMenu.StartTime.IsZero() {
		db = db.Where("create_time >= ?", selectSysMenu.StartTime)
	}
	if !selectSysMenu.EndTime.IsZero() {
		db = db.Where("create_time <= ?", selectSysMenu.EndTime)
	}
	db = db.Where("sm.deleted = '0'").Order("sm.parent_id").Order("sm.order_num").Order("sm.create_time desc")
	err = db.Find(&sysMenuList).Error
	err = errs.ErrNotFound(err)
	return
}

func GetMenuById(menuId int) (sysMenu model_sys.SysMenuVo, err error) {
	db := mysql.Db().Model(&model_sys.SysMenuPo{}).Table("sys_menu sm")
	db = db.Select("sm.*,sm1.menu_name as parentName")
	db = db.Joins("left join sys_menu sm1 on sm.parent_id = sm1.menu_id")
	err = db.Where("sm.menu_id = ? and sm.deleted = '0'", menuId).First(&sysMenu).Error
	if sysMenu.ParentId == 0 {
		sysMenu.ParentName = "主类目"
	}
	err = errs.ErrNotFound(err)
	return
}

func AddSysMenu(sysMenu model_sys.SysMenuPo) (int, error) {
	err := mysql.Db().Create(&sysMenu).Error
	return sysMenu.MenuId, errs.ErrNotFound(err)
}

func EditSysMenu(sysMenu model_sys.SysMenuPo) (int64, error) {
	res := mysql.Db().Updates(&sysMenu)
	return res.RowsAffected, errs.ErrNotFound(res.Error)
}

func IsChildren(menuId string) (bool, error) {
	var sysMenuList []model_sys.SysMenuVo
	db := mysql.Db().Model(&model_sys.SysMenuPo{})
	err := db.Where("parent_id = ?", menuId).Find(&sysMenuList).Error
	return len(sysMenuList) > 0, errs.ErrNotFound(err)
}

// RemoveSysMenu 逻辑删除
func RemoveSysMenu(menuIds string, uk string) (int64, error) {
	db := mysql.Db().Model(&model_sys.SysMenuPo{}).Where("menu_id in (?)", menuIds)
	res := db.Updates(&model_sys.SysMenuPo{BaseEntity: model.BaseEntity{UpdateTime: utils.NewTimeNow(), UpdateBy: uk, Deleted: "1"}})
	return res.RowsAffected, errs.ErrNotFound(res.Error)
}

// DeletedSysMenu 删除
func DeletedSysMenu(menuIds string) (int64, error) {
	db := mysql.Db().Model(&model_sys.SysMenuPo{}).Where("menu_id in (?)", menuIds)
	res := db.Delete(&model_sys.SysMenuPo{})
	return res.RowsAffected, errs.ErrNotFound(res.Error)
}

func GetPermissionsByUserId(userId int64) (sysMenus []model_sys.SysMenuVo, err error) {
	db := mysql.Db().Model(&model_sys.SysMenuPo{}).
		Table("sys_menu sm").
		Select("sm.*").
		Joins("inner join sys_role_menu srm on srm.menu_id = sm.menu_id").
		Joins("inner join sys_user_role sur on sur.role_id = srm.role_id")
	err = db.Where("sur.user_id = ? and sm.deleted = '0'", userId).Find(&sysMenus).Error
	err = errs.ErrNotFound(err)
	return
}
